package com.example.boot.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONArray;
import org.json.JSONObject;

import com.example.boot.ResultEnum.ResultEnum;
import com.example.boot.exception.BootException;

public class AddressMain {
	/**
	 * 距離
	 * 
	 * @param origin
	 * @param destination
	 * @return
	 * @throws Exception
	 */
	public static String loadDistance(String origin, String destination) throws Exception {
		String url = "http://restapi.amap.com/v3/direction/walking";
		String query = "key=086a77e01a6b78cc3972eecbe781de6c&origin=" + origin + "&destination=" + destination
				+ "&output=JSON";
		String urlGDDT = urlGDDT(url, query);
		JSONObject json = new JSONObject(urlGDDT);
		System.out.println(json.getString("status"));
		System.out.println("距离: "+json.toString());
		if(!json.getString("status").equals("1")){
			throw new BootException(ResultEnum.TOO_FAR_AWAY);
		}
		JSONObject j = json.getJSONObject("route");
		JSONArray jsonArray = j.getJSONArray("paths");
		JSONObject object = (JSONObject) jsonArray.get(0);
		String mi = object.getString("distance");
		return mi;
	}

	/**
	 * 商店经纬度
	 * 
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public static String  local(String address) throws Exception {
		String url = "http://restapi.amap.com/v3/geocode/geo";
		String query  = "key=086a77e01a6b78cc3972eecbe781de6c&address="+address+"&output=JSON";
		String urlGDDT = urlGDDT(url, query);
		JSONObject obj = new JSONObject(urlGDDT);
		System.out.println("商店经纬度: "+obj.toString());
		System.out.println(obj.getString("status"));
		if(!obj.getString("status").equals("1")){
			throw new BootException(ResultEnum.TOO_FAR_AWAY);
		}
//		System.out.println(obj.toString());
		JSONArray jsonArray = obj.getJSONArray("geocodes");
	
		JSONObject object = (JSONObject) jsonArray.get(0);
		return object.getString("location");
	}

	public static String urlGDDT(String url, String query) throws Exception {
		URL restURL = new URL(url);
		/*
		 * 此处的urlConnection对象实际上是根据URL的请求协议(此处是http)生成的URLConnection类
		 * 的子类HttpURLConnection
		 */
		HttpURLConnection conn = (HttpURLConnection) restURL.openConnection();
		// 请求方式
		conn.setRequestMethod("POST");
		// 设置是否从httpUrlConnection读入，默认情况下是true;
		// httpUrlConnection.setDoInput(true);
		conn.setDoOutput(true);
		// allowUserInteraction 如果为 true，则在允许用户交互（例如弹出一个验证对话框）的上下文中对此 URL 进行检查。
		conn.setAllowUserInteraction(false);

		PrintStream ps = new PrintStream(conn.getOutputStream());
		ps.print(query);

		ps.close();

		BufferedReader bReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

		String line, resultStr = "";

		while (null != (line = bReader.readLine())) {
			resultStr += line;
		}
		bReader.close();
		return resultStr;
	}

	public static void main(String[] args) {

		try {
			// String urlGDDT =
			// urlGDDT("http://restapi.amap.com/v3/geocode/geo",
			// "key=086a77e01a6b78cc3972eecbe781de6c&address=广东省深圳市宝安区宝体地铁站B出口&output=JSON");
			//

			String load = loadDistance("113.880641,22.560741", "113.881568,22.570538");
			
			System.out.println();
//			JSONArray jsonArray = json.getJSONArray("route");
//			JSONObject object = (JSONObject) jsonArray.get(0);
////			
//			JSONArray jsonArray2 = object.getJSONArray("paths");
//			JSONObject object2 = (JSONObject) jsonArray2.get(0);
//			System.out.println(object2.get("distance"));

//			 System.out.println(j);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
